package com.jmp.api.service.impl;

import com.jmp.api.dto.MessagePushDto;
import com.jmp.api.service.MessageService;
import com.jmp.api.service.SeeRecordBaseService;
import com.jmp.base.dto.GoodsGroupBuyingDto;
import com.jmp.base.dto.MessagePushType;
import one.stand.dto.UserDto;
import one.stand.enums.SeeRecordEnums;
import one.stand.mapper.*;
import one.stand.model.*;
import one.stand.service.BusinessCardBaseService;
import one.stand.service.GoodsBaseService;
import one.stand.service.user.UserAdminService;
import one.stand.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author cjr
 */
@Service
public class SeeRecordBaseServiceImpl implements SeeRecordBaseService {
    @Autowired
    private BusinessCardPublicMapper businessCardPublicMapper;
    @Autowired
    private MessageService messageService;
    @Autowired
    private BusinessCardBaseService businessCardBaseService;
    @Autowired
    private UserAdminService userAdminService;
    @Autowired
    private GoodsBaseService goodsBaseService;
    @Autowired
    private ArticleMapperExt articleMapperExt;

    @Autowired
    private SeeRecordMapperExt seeRecordMapperExt;
    @Autowired
    private SeeRecordHideMapperExt seeRecordHideMapperExt;

    @Override
    @Async("asyncServiceExecutor")
    public void card(Integer userId, Integer cardId, Integer itmeId, boolean isWechat) {
        addSeeRecord(userId, cardId, SeeRecordEnums.CARD.getCode(), itmeId);

        if (isWechat) {
            // 客户来访提醒微信推送提醒
            UserDto user = userAdminService.get(userId);
            BusinessCard card = businessCardBaseService.getCustomer(cardId);
            String[] fillData = {user.getName()};
            MessagePushDto messageDto = MessagePushDto.builder()
                    .type(MessagePushType.CUSTOMER_ACCESS)
                    .toId(card.getUserId())
                    .fillData(fillData)
                    .build();
            messageService.push(messageDto);
        }
    }

    @Override
    @Async("asyncServiceExecutor")
    public void goodsList(Integer userId, Integer cardId) {
        addSeeRecord(userId, cardId, SeeRecordEnums.PRODUCT.getCode(), null);
    }

    @Override
    @Async("asyncServiceExecutor")
    public void goods(Integer userId, Integer cardId, Integer itmeId, Integer type, Integer groupId) {
        addSeeRecord(userId, cardId, SeeRecordEnums.PRODUCT.getCode(), itmeId);

        if (type == 1) {//拼图
            GoodsGroupBuyingDto goodsGroupBuyingDto = goodsBaseService.getGroup(groupId);
            BusinessCard businessCardModel = businessCardBaseService.getCustomer(cardId);
            UserDto user = userAdminService.get(userId);
            String[] fillData = {goodsGroupBuyingDto.getTitle()};
            MessagePushDto messageDto = MessagePushDto.builder()
                    .type(MessagePushType.SEE_GOODS_GROUP)
                    .toId(businessCardModel.getUserId())
                    .title(user.getName() + "用户正在访问您的拼团")
                    .fillData(fillData)
                    .build();
            messageService.push(messageDto);
        } else {
            Goods goodsModel = goodsBaseService.get(itmeId);
            BusinessCard businessCardModel = businessCardBaseService.getCustomer(cardId);
            UserDto user = userAdminService.get(userId);
            String[] fillData = {user.getName(), goodsModel.getGoodsName()};
            MessagePushDto messageDto = MessagePushDto.builder()
                    .type(MessagePushType.SEE_GOODS)
                    .toId(businessCardModel.getUserId())
                    .fillData(fillData)
                    .build();
            messageService.push(messageDto);
        }
    }

    @Override
    @Async("asyncServiceExecutor")
    public void goodsJxs(Integer userId, Integer cardId, Integer itmeId) {
        addSeeRecord(userId, cardId, SeeRecordEnums.PRODUCT.getCode(), itmeId);

        Goods goodsModel = goodsBaseService.get(itmeId);
        BusinessCard businessCardModel = businessCardBaseService.getCustomer(cardId);
        UserDto user = userAdminService.get(userId);
        String[] fillData = {user.getName(), goodsModel.getGoodsName()};
        MessagePushDto messageDto = MessagePushDto.builder()
                .type(MessagePushType.SEE_GOODS_JXS)
                .toId(businessCardModel.getUserId())
                .fillData(fillData)
                .build();
        messageService.push(messageDto);

    }

    @Override
    @Async("asyncServiceExecutor")
    public void goodsCount(Integer goodsId) {
        if (Objects.nonNull(goodsId)) {
            seeRecordMapperExt.updateRecordRanking(SeeRecordEnums.PRODUCT.getCode(), goodsId);
        }
    }

    @Override
    @Async("asyncServiceExecutor")
    public void article(Integer userId, Integer cardId, Integer itmeId) {
        addSeeRecord(userId, cardId, SeeRecordEnums.ARTICLE.getCode(), itmeId);
    }

    @Override
    @Async("asyncServiceExecutor")
    public void website(Integer userId, Integer cardId, Integer itmeId) {
        addSeeRecord(userId, cardId, SeeRecordEnums.OFFICIAL_WEBSITE.getCode(), itmeId);
    }

    @Override
    @Async("asyncServiceExecutor")
    public void lead(Integer userId, Integer cardId, Integer articleId) {
        addSeeRecord(userId, cardId, SeeRecordEnums.LEAD.getCode(), articleId);

        articleMapperExt.updateView(articleId);

        BusinessCard businessCardModel = businessCardBaseService.getCustomer(cardId);
        UserDto user = userAdminService.get(userId);
        Article article = articleMapperExt.selectByPrimaryKey(articleId);
        String[] fillData = {user.getName(), article.getTitle()};
        MessagePushDto messageDto = MessagePushDto.builder()
                .type(MessagePushType.ARTICLE_VIEW)
                .fromId(userId)
                .toId(businessCardModel.getUserId())
                .fillData(fillData)
                .pageParams(new Object[]{articleId})
                .build();
        messageService.push(messageDto);
    }

    @Override
    public void remove(Integer userId, Integer cardId) {
        seeRecordMapperExt.delSeeCard(userId, null, cardId);
    }

    @Override
    public void hide(Integer cardId, Integer viewUserId) {
        SeeRecordHide seeRecordHide = new SeeRecordHide();
        seeRecordHide.setCardId(cardId);
        seeRecordHide.setViewUserId(viewUserId);
        seeRecordHideMapperExt.insertSelective(seeRecordHide);
    }

    /**
     * 添加查看记录
     *
     * @param userId  查看人ID
     * @param cardId  查看入口名片ID/查看对象ID
     * @param seeType 1:名片，2:产品，3：官网,4:查看文章
     * @param itmeId  查看对象ID
     * @return
     */
    private void addSeeRecord(Integer userId, Integer cardId, Integer seeType, Integer itmeId) {
        //添加查看记录
        SeeRecord model = new SeeRecord();
        if (null != userId) {
            model.setUserId(userId);
        }
        if (null != seeType) {
            model.setSeeType(seeType);
        }
        if (null != itmeId) {
            model.setItmeId(itmeId);
        }
        if (null != cardId) {
            model.setCardId(cardId);

            businessCardPublicMapper.addSeeNum(cardId);

            seeRecordHideMapperExt.delete(cardId, model.getUserId());
        }
        model.setRanking(seeRecordMapperExt.selectRanking(model.getUserId(), model.getSeeType(), model.getCardId()) + 1);
        model.setCreateTime(DateUtil.currentDate());
        seeRecordMapperExt.insertSelective(model);

    }
}
